5. What is git and how can I use it?

What I would like to show you about git

  • How to use a terminal
  • Git
    • What is it good for?
    • What is it?
    • What can it do?
  • Set-up for your computer
    • GUI/terminal
    • R Studio & git
    • SSH
    • Set name & email address
  • Your first repo
    • Github and GitLab
    • In R Studio
    • gitignore
    • README
  • Workflow
    • Add, Commit, Push
    • Diff
    • Merge, Branches, Tagging… (all the cool stuff)
    • What happens if something goes wrong? (And it will.)

What we have time for

  • How to use a terminal
  • Git
    • What is it good for?
    • What is it?
    • What can it do?
  • Set-up for your computer
    • GUI/terminal
    • R Studio & git
    • SSH
    • Set name & email address
  • Your first repo
    • Github and GitLab
    • In R Studio
    • gitignore
    • README
  • Workflow
    • Add, Commit, Push
    • Diff
    • Merge, Branches, Tagging… (all the cool stuff)
    • What happens if something goes wrong? (And it will.)

What is it good for?

Version control:

  • Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later.
  • History of all changes (who, what, when).
  • Helps to avoid mistakes (working on the wrong version, deleting, …).
  • Merging changes of multiple collaborators in one file.

What is it?

What can it do?

  • A lot! Which is why we only need a part of its functionality.
  • Working on one product in (large) teams.
  • Working on things that can break.
  • git can only integrate and show changes in text files.
  • binary files (images, etc.) can be tracked and uploaded but changes cannot be shown.
  • Track changes for MS Word is improving.

Set-up for your computer

R Studio & git

R Studio & git

Tools ➤ Global Options ➤ Git/SVN.

Make sure the first box is ticked and the “git.exe” (Windows) is included in the first box.

Set name & email address

  • Open the Terminal in R Studio.
  • Set an email address and user name for git.

Set name & email address

  • Open the Terminal in R Studio.
  • Set an email address and user name for git.
git config --global user.email "myemail@email.com"
git config --global user.name "My commit name"

My first repo

Github

Github

New Repository

Github

New Repository

Github

New Repository

Github

Settings

Github

Settings

Github

Settings

Github

Clone It!

In R Studio

File ➤ New Project ➤ Version Control ➤ Git

In R Studio

  • You will have to type in you user name and password for github.
  • Initializes a local git repository with an R project (opening a clean R Studio session when opeing).
  • You can see the README file from github.
  • Adds a .gitignore file.


Task IV

  • Make a new repository.
  • Clone it using R Studio to make a local repository.

gitignore

  • Specifies intentionally untracked files to ignore.
  • Each line in a gitignore file specifies a pattern.
  • R Studio pre-specifies some useful patterns.
  • For R Markdown: Cache files! .tiff, .eps, .rdb, .rdx


README

README

  • Tell other people (and yourself in a year) why your project is useful, what they can do with your project, and how they can use it.
  • On github default README files are Markdown files!

Task V

  • Write a (short) README file for your test repository.
  • Use Markdown formatting.

Git Workflow

Do some work

Git Add


git add .gitignore myfirstrepo.Rproj



git can do autocomplete for file names!

Git Commit


git add .gitignore myfirstrepo.Rproj
git commit -m "My first commit"



Commits always have a commit message.

Commit message


Git Push


git add .gitignore myfirstrepo.Rproj
git commit -m "My first commit"
git push

Congrats! You have done it! Now local and remote repositories are up to date!

Task VI

  • Add your unstages files.
  • Commit the changes.
  • Push to the remote repository.

Git Pull

Before you start working on the project the next time:


git pull


Pull, work some more, repeat.

What changed since the last commit?

git diff

What happens if something goes wrong? (And it will.)

What happens if something goes wrong? (And it will.)

  • Remember: You cannot break things.
  • Most likely you have a merge conflict.

Push Conflict

Merge Conflict

Merge Conflict

  • Resolve the conflict (Choose which changes to keep).
  • Commit, and Push.

Summary

  • Add, commit, push, pull.
  • Use it!
  • git documentation and error tracking are great!

References